package com.appcenter.application.service;

import com.appcenter.application.input.Role.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.RolesMapper;
import com.appcenter.application.model.Roles;
import com.appcenter.application.output.Role.*;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RoleService {
    @Autowired
    RolesMapper rolesMapper;

    @Autowired
    AppsMapper appsMapper;

    public CreateRoleOutput CreateRole(CreateRoleInput input) {
        CreateRoleOutput output = new CreateRoleOutput();
        int CodeCount = rolesMapper.getCodeCount(input.getItem().getAppId(), input.getItem().getRoleCode()).size();
        if (CodeCount > 0) {
            throw new ErrorJsonResult("1000061", "角色编号已存在！");
        }
        int NameCount=rolesMapper.getCodeCount(input.getItem().getAppId(), input.getItem().getRoleName()).size();
        if (NameCount > 0) {
            throw new ErrorJsonResult("1000062", "角色名称已存在！");
        }
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        Roles role=new Roles();
        role.setId(uuid);
        role.setAppId(input.getItem().getAppId());
        role.setAppId(input.getItem().getAppId());
        role.setRoleCode(input.getItem().getRoleCode());
        role.setRoleName(input.getItem().getRoleName());
        role.setDescription(input.getItem().getDescription());
        rolesMapper.insertSelective(role);
        output.setItem(role);
        return output;
    }

    public DeleteRoleOutput DeleteRole(DeleteRoleInput input) {
        DeleteRoleOutput output = new DeleteRoleOutput();
        rolesMapper.deleteByPrimaryKey(input.getItem().getId());
        return output;
    }

    public EditRoleOutput EditRole(EditRoleInput input) {
        EditRoleOutput output = new EditRoleOutput();
        int CodeCount = rolesMapper.getCodeCountNoId(input.getItem().getAppId(), input.getItem().getRoleCode(),input.getItem().getId()).size();
        if (CodeCount > 0) {
            throw new ErrorJsonResult("1000061", "角色编号已存在！");
        }
        int NameCount=rolesMapper.getCodeCountNoId(input.getItem().getAppId(), input.getItem().getRoleName(),input.getItem().getId()).size();
        if (NameCount > 0) {
            throw new ErrorJsonResult("1000062", "角色名称已存在！");
        }
        Roles role=rolesMapper.selectByPrimaryKey(input.getItem().getId());
        role.setAppId(input.getItem().getAppId());
        role.setAppId(input.getItem().getAppId());
        role.setRoleCode(input.getItem().getRoleCode());
        role.setRoleName(input.getItem().getRoleName());
        role.setDescription(input.getItem().getDescription());
        rolesMapper.updateByPrimaryKey(role);
        output.setItem(role);
        return output;
    }

    public GetRolesOutput GetRoles(GetRolesInput input) {
        GetRolesOutput output = new GetRolesOutput();
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        List<Roles> ls = rolesMapper.searchRolesList(input);
        PageInfo<Roles> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Roles>(page.getList(),(int)page.getTotal()));
        return output;
    }

    public PageLoadOutput PageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setApps(appsMapper.selectAll());
        output.setItems(GetRoles(input).getItems());
        return output;
    }


}
